# rm(list = ls())

library(grid)
#setwd("/Users/ad_bleveck/Dropbox/Ally Rep Paper (w:LeVeck)/International Interactions/Revise and Resubmit/Replication/replication_final")
##point estimates, in a n.variables, n.variables x n.models
#coef.matrix <- as.matrix(read.csv("allGainCoefs.csv", header=FALSE))

##standard error matrix, n.variables x n.models
#se.matrix <- as.matrix(read.csv("allGainSEs.csv", header=FALSE))

##variable names
varnames<- c("Reliability", "Violation History", "Major Power", "Democracy",
							"Lagged Number \n of Alliances", "Lagged Number \n of Alliances^2"
						)


##create a csv file with the table, so we can create the latex table with 
## each panel has at most six models, plotted in pairs.
## in each pair, solid circles will be the models with "law change" in the specification
## empty circles, those without "law change"
                 
##we are making a list, define it first as empty
Y1 <- vector(length=0,mode="list")
#estimates with law change (in the 4th to 6th columns)
Y1$estimate <- coef.matrix[,4:6]
##95% confidence intervals
Y1$lo <- coef.matrix[,4:6]-qnorm(0.975)*se.matrix[,4:6]
Y1$hi <- coef.matrix[,4:6]+qnorm(0.975)*se.matrix[,4:6]
##90% confidence intervals
Y1$lo1 <- coef.matrix[,4:6]-qnorm(0.95)*se.matrix[,4:6]
Y1$hi1 <- coef.matrix[,4:6]+qnorm(0.95)*se.matrix[,4:6]
##name the rows of Y1 estimate
rownames(Y1$estimate) <- varnames ##no intercept
                 
                 
#estimates without law change
Y2 <- vector(length=0,mode="list")
Y2$estimate <- coef.matrix[,1:3]
Y2$lo <- coef.matrix[,1:3]-qnorm(.975)*se.matrix[,1:3]
Y2$hi <- coef.matrix[,1:3]+qnorm(.975)*se.matrix[,1:3]
Y2$lo1 <- coef.matrix[,1:3]-qnorm(.95)*se.matrix[,1:3]
Y2$hi1 <- coef.matrix[,1:3]+qnorm(.95)*se.matrix[,1:3]
rownames(Y2$estimate) <- varnames
                 
                 
##Plot estimates in a single columns
source("plotReg.R")
pdf(file="Figure_2.pdf",width=6,height=10)
## create the graph (do not print it yet)
tmp <- plot.reg(Y1,Y2,
                                        #the lists
                                        #the model labels
                                        #                label.x=c("Baseline Model","Plus Cubic Polynomial\nto Account for Duration\nBetween Alliance Gains",
                                        #                  "Plus Cubic Polynomial\ninto Account for Decay\nin Effect a Violation on Alliance Gains"),
                label.x=c("Model Specification 1\n(Baseline Model)", "Model Specification 2\n(Plus Duration\nDependence)", "Model Specification 3\n(Plus Decay)"),
                ## reference lines
                refline=c(0,0,0,0,0,0),
                ## space left in the bottom (for the x-axis labels)
                hlast=.15,
                ## print the graph?
                print=FALSE,
                ## line width / character size multiplier
                lwd.fact=1.3,
                ## length of the cross- hairs
                length.arrow=unit(0,"mm"),
                ## legend
                ## legend=c("without law change","with law change"),
                ## widths: variable names, plot size, y-axis
                widths=c(.6,.4,.3),
                ## rotation of the variable name labes
                rot.label.y=0,
                ## justification of the variable name labels
                just.label.y="right",
                ## position (x-axis) of the variable name labels)
                pos.label.y=0.95,
                ## size of the symbol
                pch.size=0.6,expand.factor=.2,expand.factor2=0.1,
                ##legend
                legend=c("Model A\nReputation coded as\nUnreliability","Model B\nReputation coded as\nViolation History"),
                leg.mult=.7,
                ##legend font size
                leg.fontsize=11,
                v.grid=TRUE,
                #yaxis.at=list(
                #NULL,
                #NULL,
                #seq(-.1,.1,.05),
                #seq(-.2,.2,.1),
                #seq(-.2,.2,.1),
                #seq(-1,1,.5)
                #)
                )
## we rotate the labels of the x-axis 45 degrees. The grid utilities allow
## this modification "on the fly", and it is easy if you are careful at naming the paths
tmp <- editGrob(tmp,gPath("xaxis","labels"),rot=45,just="right",gp=gpar(lineheight=.75))
##tmp is the object we have just created,"xaxis" is the name of element in the object with the x-axis
##elements, and "labels" is the actual object in xaxis that we want to rotate
##just is the justification of the text
grid.draw(tmp) ## print the graph
graphics.off() ## close the graphics windows
